package com.dart.commons.base.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.dart.commons.base.mybatis.BaseMapper;
import com.dart.commons.base.web.util.PageParamsThreadLocal;
import com.dart.framework.common.DataUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


/**
 * 基础Service<br>
 * Service实现类可以继承该基础类，该类封装了CRUD操作<br>
 * 该Service基础类不是必须的
 * 
 * @param <T> Mapper对应的实体对象
 * @param <PK> 主键的类型
 * @作者：杨浩泉
 * @日期：2016年9月23日
 */
public class BaseService<T, PK> implements IService<T, PK> {
	
	//private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private BaseMapper<T, PK> baseMapper ;
	
	/**
	 * 返回结果：true或false
	 * @param result
	 * @return
	 */
	protected boolean returnBoolean(int result) {
		return (result >= 1) ? true : false;
	}

	@Override
	public boolean insert(T entity) {
		return returnBoolean(this.baseMapper.insert(entity));
	}

	@Override
	public boolean insertSelective(T entity) {
		return returnBoolean(this.baseMapper.insertSelective(entity));
	}

	@Override
	public boolean insertBatch(List<T> entityList) {
		return returnBoolean(this.baseMapper.insertList(entityList));
	}
	
	@Override
	public boolean inserOrUpdate(T entity) {
		return returnBoolean(this.baseMapper.inserOrUpdate(entity));
	}

	@Override
	public boolean delete(PK id) {
		return returnBoolean(this.baseMapper.deleteByPrimaryKey(id));
	}

	@Override
	public boolean deleteSelective(T entity) {
		return returnBoolean(this.baseMapper.delete(entity));
	}

	@Override
	public boolean deleteBatchIds(List<PK> idList) {
		return returnBoolean(this.baseMapper.deleteBatchIds(idList));
	}

	@Override
	public boolean update(T entity) {
		return returnBoolean(this.baseMapper.updateByPrimaryKey(entity));
	}

	@Override
	public boolean updateSelective(T entity) {
		return returnBoolean(this.baseMapper.updateByPrimaryKeySelective(entity));
	}

	@Override
	public boolean update(T entity, T whereEntity) {
		return returnBoolean(this.baseMapper.update(entity, whereEntity));
	}

	@Override
	public boolean updateSelective(T entity, T whereEntity) {
		return returnBoolean(this.baseMapper.updateSelective(entity, whereEntity));
	}

	@Override
	public boolean updateBatchById(List<T> entityList) {
		return returnBoolean(this.baseMapper.updateBatchById(entityList));
	}

	@Override
	public T selectById(PK id) {
		return this.baseMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<T> selectBatchIds(List<PK> idList) {
		return this.baseMapper.selectBatchIds(idList);
	}

	@Override
	public T selectOne(T entity) {
		return this.baseMapper.selectOne(entity);
	}

	@Override
	public int selectCount(T entity) {
		return this.baseMapper.selectCount(entity);
	}

	@Override
	public List<T> selectList(T entity, String orderByField) {
		return this.baseMapper.selectList(entity, orderByField);
	}

	@Override
	public List<T> selectList(T entity) {
		return this.baseMapper.select(entity);
	}

	@Override
	public PageInfo<T> selectPage(T entity, String orderByField) {
		Page<T> page = PageHelper.startPage(PageParamsThreadLocal.getPageNum(), PageParamsThreadLocal.getPageSize(), orderByField) ;
		page.clear();
		this.baseMapper.selectPage(entity);
		return new PageInfo<T>(page) ;
	}

	@Override
	public PageInfo<T> selectPage(T entity) {
		return selectPage(entity, null);
	}

	@Override
	public PageInfo<T> selectPageByMap(Map<String, Object> params) {
		Map<String, Object> pageMap = new HashMap<String, Object>() ;
		if (DataUtil.isEmpty(params.get("pageNum"))) {
			pageMap.put("pageNum", 1);
		}
		if (DataUtil.isEmpty(params.get("pageSize"))) {
			pageMap.put("pageSize", 10);
		}
		if (DataUtil.isEmpty(params.get("orderBy"))) {
			pageMap.put("orderBy", "id desc");
		}
		
		Page<T> page = PageHelper.startPage(pageMap) ;
		baseMapper.selectPageByMap(params) ;
		return new PageInfo<T>(page) ;
	}

}
